/**
 * Jenkins Demo Application
 * 这是一个简单的 Node.js 应用示例
 */

const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;

// 健康检查端点
app.get('/health', (req, res) => {
    res.json({
        status: 'healthy',
        timestamp: new Date().toISOString(),
        version: '1.0.0'
    });
});

// 首页
app.get('/', (req, res) => {
    res.send(`
        <h1>Welcome to Jenkins Demo App!</h1>
        <p>This application was built and deployed by Jenkins CI/CD pipeline.</p>
        <ul>
            <li>Version: 1.0.0</li>
            <li>Environment: ${process.env.NODE_ENV || 'development'}</li>
            <li>Build Number: ${process.env.BUILD_NUMBER || 'N/A'}</li>
        </ul>
    `);
});

// API 端点
app.get('/api/info', (req, res) => {
    res.json({
        app: 'jenkins-demo-app',
        version: '1.0.0',
        buildNumber: process.env.BUILD_NUMBER || 'local',
        buildTime: process.env.BUILD_TIME || new Date().toISOString()
    });
});

// 启动服务器
if (require.main === module) {
    app.listen(PORT, () => {
        console.log(`🚀 Server is running on port ${PORT}`);
        console.log(`📍 Health check: http://localhost:${PORT}/health`);
        console.log(`📍 API info: http://localhost:${PORT}/api/info`);
    });
}

module.exports = app;
